<?php

/*
    Templights - lightweight PHP templating
    Copyright (C) 2009 Gregor Mazovec <gregor.mazovec@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'base.php';

/**
 * This function is used for overriding content of specific template block
 *
 * @name array $tpl Template - list of string nodes
 * @name string $name Block name
 * @name array $new New content of template block (list of nodes)
 * @return array Changed list of sting nodes
 */
function tpl_extend_block ($tpl, $name, $new) {
    foreach ($tpl as $block => $node)
        if ($block === $name) {
            $tpl[$name] = $new;
            break;
        }
        else if (is_array($node))
            $tpl[$block] = tpl_extend_block($node, $name, $new);
    return $tpl;
}

/**
 * Extend template from another template
 *
 * @name array $tpl Template - list of string nodes
 * @name array $blocks Blocks extending parent template
 * @return array Extended template
 */
function tpl_extend ($tpl, $blocks) {
    foreach ($blocks as $name => $block)
        if (is_array($block))
            $tpl = tpl_extend_block(&$tpl, $name, $block);
    return $tpl;
}

/**
 * Wrapper function for creating template form template code
 *
 * @name $code Template code
 * @return array Template - list of string nodes
 */
function tpl_compile ($code) {
    return tpl_parse(tpl_token($code));
}

/**
 * Function for evaluating block variables within specified context.
 *
 * @todo pass by reference optimization
 *
 * @name array $tpl Template - list of string nodes
 * @name array $context Template context
 * @return array Evaluated template
 */
function tpl_eval ($tpl, $context) {
    $_tpl = array();
    foreach ($tpl as $name => $block)
        if (is_string($name))
            if ($name{0} == '@') {
                if ($val = (string)$context[substr($name, 1)])
                    if (ctype_upper($val)) $_tpl[$val] = $block;
                    else {
                        $parent = tpl_load($val);
                        $block = tpl_extend($parent, $block);
                        $_tpl[] = tpl_eval($block, $context);
                    }
            } else $_tpl[$name] = tpl_eval($block, $context);
        else $_tpl[] = $block;
    return $_tpl;
}

/**
 * Wrapper function for dumping template code into a file
 *
 * @name array $tpl Template - list of string nodes
 * @name string $file Filename of dump file
 * @return bool
 */
function tpl_dump ($tpl, $file = 'output.tpl') {
    return file_put_contents($file, tpl_tostring($tpl));
}
